package com.dnake.utils;

import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dnake.WeKerApplication;
import com.dnake.panel.R;
import com.dnake.widget.ErrorToastView;
import com.dnake.widget.SuccessToastView;


/**
 * Created by joyin on 16-four-three.
 */
public class ToastUtil {
    static SuccessToastView successToastView;
    static ErrorToastView errorToastView;
    static Toast toast;
    private ToastUtil() {

    }

    private static Toast mToast;

    public static void show(int resId) {
        show(WeKerApplication.getContext().getString(resId));
    }

    public static void show(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(WeKerApplication.getContext(), msg, Toast.LENGTH_SHORT);
            LinearLayout layout = (LinearLayout) mToast.getView();
            layout.setBackgroundResource(R.drawable.shape_bg_toast);
            TextView v = (TextView) mToast.getView().findViewById(android.R.id.message);
            v.setPadding(20,8,20,8);
            v.setTextColor(Color.WHITE);
            v.setTextSize(24);
            mToast.setGravity(Gravity.BOTTOM,0,0);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }

    /**
     * 长时间显示
     * @param msg
     */
    public static void showLong(String msg) {
        if (mToast == null) {
            mToast = Toast.makeText(WeKerApplication.getContext(), msg, Toast.LENGTH_LONG);
            LinearLayout layout = (LinearLayout) mToast.getView();
            layout.setBackgroundResource(R.drawable.shape_bg_toast);
            TextView v = (TextView) mToast.getView().findViewById(android.R.id.message);
            v.setPadding(20,8,20,8);
            v.setTextColor(Color.WHITE);
            v.setTextSize(24);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }
    public static void showSuccessView(String msg){
        if (toast==null){
            toast =  new NewToast(WeKerApplication.getContext(), R.style.myToastUpDown);
        }
        View layout = LayoutInflater.from(WeKerApplication.getContext()).inflate(R.layout.layout_success_toast, null, false);
        TextView text = (TextView) layout.findViewById(R.id.toastMessage);
        text.setText(msg);
        successToastView = (SuccessToastView) layout.findViewById(R.id.successView);
        successToastView.startAnim();
        text.setBackgroundResource(R.drawable.success_toast);
        text.setTextColor(Color.parseColor("#FFFFFF"));
        toast.setView(layout);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT,0,0);
        toast.show();
    }
    public static void showFailView(String msg){
//        Toast toast =  new NewToast(WeKerApplication.getContext(), R.style.myToastUpDown);
        if (toast==null){
            toast =  new NewToast(WeKerApplication.getContext(), R.style.myToastUpDown);
        }
        View layout = LayoutInflater.from(WeKerApplication.getContext()).inflate(R.layout.layout_error_toast, null, false);
        TextView text = (TextView) layout.findViewById(R.id.toastMessage);
        text.setText(msg);
        errorToastView = (ErrorToastView) layout.findViewById(R.id.errorView);
        errorToastView.startAnim();
        text.setBackgroundResource(R.drawable.error_toast);
        text.setTextColor(Color.parseColor("#FFFFFF"));
        toast.setView(layout);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM| Gravity.RIGHT,0,0);
        toast.show();
    }
}
